<template>
  <el-dialog
    v-dialogDrag
    :title="dialogTitle"
    :visible="dialogVisible"
    class="dialogStyleSmall"
    :before-close="cancel"
  >
    <el-row>
      <el-col :span="1.5">
        <el-button size="small" @click="downLoadTemp">下载模板</el-button>
      </el-col>
    </el-row>
    <el-row style="display:block;margin:10px auto;">
      <el-upload
        ref="upload"
        drag
        class="upload-demo"
        :on-success="onUploadSuccess"
        :on-change="handleChange"
        :action="BASE_API"
        :headers="myHeaders"
        :with-credentials="true"
        :auto-upload="false"
        :limit="1"
        accept=".xlsx,.xls"
      >
        <!-- <el-button size="small" >点击上传</el-button>
          <div slot="tip" class="el-upload__tip">且不超过500kb</div> -->
        <i class="el-icon-upload" />
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      </el-upload>
    </el-row>
    <template slot="footer">
      <el-button @click="cancel">保 存</el-button>
      <el-button @click="cancel">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
// import { getExcelTemplate } from '@/api/system/user'
export default {
  name: 'ToLead',
  data() {
    return {
      dialogTitle: '',
      dialogVisible: false,
      templateName: '人员信息模板.xlsx',
      /** 文件上传 *******start*/
      BASE_API: process.env.VUE_APP_BASE_API + '/sys/user/importDatas',

      // 上传文件的请求头
      myHeaders: { 'token': this.$store.getters.token }
    }
  },
  methods: {
    openToLead() {
      this.dialogTitle = '导入数据'
      this.dialogVisible = true
    },
    downLoadTemp() {
      this.downloadUser(this.templateName, false)
      // getExcelTemplate().then(resp => {
      //   if (resp) {
      //     this.download(resp.message)
      //   }
      // })
    },
    /** ************ 附件上传 start*******************************************************/
    // 文件上传成功的回调
    onUploadSuccess(res, file) {
      if (res) {
        this.msgSuccess('上传成功')
        this.dialogVisible = false
        this.emptyUpload()
      } else {
        this.msgError(res.message)
        this.emptyUpload()
      }
    },
    handleChange(file) {
      const _this = this
      if (file.status !== 'success') {
        this.$nextTick(() => {
          _this.$refs.upload.submit()
        })
      }
    },
    /**
     * 清空上传组件
     */
    emptyUpload() {
      this.$refs.upload.clearFiles()
    },
    // 对话框取消按钮
    cancel() {
      this.$refs.upload.clearFiles()
      this.dialogVisible = false
      this.$emit('parentGetList')
    }
  }
}
</script>

<style>

</style>
